package com.shujia.uerService;

import com.shujia.Dao.userDao;
import com.shujia.JdbcUtil.MD5;
import com.shujia.user.Message;
import com.shujia.user.User;

public class userService {
    userDao userDao = new userDao();
    public Message login(String username, String password){

        User u = userDao.loginon(username);
        if(u==null){
            return new Message(300,"用户不存在");
        }
        String md5Password = MD5.md5(password);
        if(!md5Password.equals(u.getPassword())){
            return new Message(300,"密码不正确");
        }
            return new Message(200,"登陆成功");


    }

    public String insertinto(String username,String password,String password1){
//        1.判断用户是否存在
        User log = userDao.loginon(username);
        if(log!=null){
            return "用户存在";
        }
        if(!password.equals(password1)){
            return "密码不一致";
        }
        String s = MD5.md5(password);
        int insertuer = new userDao().insertuer(new User(username, s));
        if(insertuer==0){
            return "注册失败";
        }
        return "注册成功";

    }

    public String updateuser(String username,String password,String newpassword,String newpassword1){
        User u = userDao.loginon(username);
        if(u==null){
            return "用户不存在";
        }
        String md5Password = MD5.md5(password);
        if(!md5Password.equals(u.getPassword())){
            return "密码不正确";
        }
        if(!newpassword.equals(newpassword1)){
            return "两次输入新密码不正确";
        }
        String s = MD5.md5(newpassword);
        int flag = userDao.update(username,s);
        if(flag==0){
            return "修改失败，请重新修改";
        }
        return "修改成功";
    }

}
